
public class HistogramGenerator {
	
	public static String printAsciiHistogram(int[] counts, int t){
		
		t=t-1;
		
		String hist = "";
		
		char[][] bars= new char[10][counts.length];
		
		int min = 2000000000, max = -1;

		for(int i=0;i<counts.length;i++){
			if(counts[i] < min)
				min=counts[i];
			if(counts[i] > max)
				max = counts[i];
		}
		
		int[] buckets=new int[bars.length];
		if(min ==0) min =1;
		double lmin = Math.log10(min);
		double lmax = Math.log10(max);
		
		double step = ((double)lmax-lmin)/bars.length;
		
		for(int i=0;i<counts.length;i++){
			double l=Math.log10(counts[i]);
			for(int j=0;j<bars.length;j++){
				if(l> lmin+j*step)
					bars[j][i] = '*';
				else bars[j][i] = '-';
			}
		}
		
		for(int i=0;i<bars.length;i++){
			String line = new String(bars[i]);
			if(t<line.length())
				hist = line.substring(0,t) + '|' + line.substring(t) + "\n" + hist ;
			else
				hist = line + "|\n" + hist;
		}
//			hist = new String(bars[i]) + "\n" + hist;
		String scale = "1 3 5 7  10   15   20   25   30   35   40   45   50   ...";
		if(counts.length<scale.length())
			scale = scale.substring(0, counts.length);
		
		if(t<scale.length())
			hist += scale.substring(0,t) + '|' + scale.substring(t);
		else 
			hist += scale;
		
		return hist;
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(printAsciiHistogram(new int[] {4370258,317872,332991,468992,614831,726970,800177,824956,805870,749993,667734,575364,480255,388096,308020,236928,178521,132413,96066,226704},5));
	}

}
